/* -----------------
* brief
* 1. this is code gen by tools
* 2. array like [a,b,c] after each field, define the ui html tag in view,
		a for title,
		b for show or not ,
		c for element html tag , currently support : check、radio、select、input、textarea、upload
* 3. XXX
*/

package wechat

import (
	wxcardlib "github.com/chanxuehong/wechat/mp/card"
	"github.com/mjiulee/lego"
)

func init() {
	lego.AddBeanToSynList(new(WxCardUserCard))
}

/* table: "tb_wx_card_user_card"
 * -----------------
 */
type WxCardUserCard struct {
	Id    int64  `xorm:"pk" json:",string"`                 // ["id","hide","hidden"]
	IfDel int    `xorm:"default 0"`        // ["deleted","hide","hidden"]
	Cdate string `xorm:"DateTime created"` // ["create","show","datetime"]
	Udate string `xorm:"DateTime updated"` // ["update","show","datetime"]
	// add your custom field here
	//userWho common.Who `xorm:"extends"`
	Status  wxcardlib.USER_CARD_STATUS `xorm:"varchar(32) NOT NULL"`                      // ["状态","show","input"]
	WeappId string `xorm:"varchar(32) NOT NULL unique(grp_uqe_card)"` // ["微信APP ID","show","input"]

	CardId              string `xorm:"varchar(32) unique(grp_uqe_card)"` //  ["卡券ID","show","input"]
	IsGiveByFriend      int    `xorm:""`                                 // ["是否转赠","show","input"]是否为转赠, 1 代表是, 0 代表否.
	FriendUserName      string `xorm:"varchar(32)"`                      // ["赠送方账号OpenID","show","input"] 赠送方账号(一个OpenID), "IsGiveByFriend"为1 时填写该参数.
	UserCardCode        string `xorm:"varchar(32) unique(grp_uqe_card)"` // ["code序列号","show","input"]code 序列号. 自定义code 及非自定义code的卡券被领取后都支持事件推送.
	OldUserCardCode     string `xorm:"varchar(64)"`                      // ["转赠前的code序列号","show","input"]转赠前的code序列号。
	OuterId             int64  `xorm:""`                                 // ["领取场景值","show","input"] 领取场景值, 用于领取渠道数据统计. 可在生成二维码接口及添加JS API 接口中自定义该字段的整型值.
	OuterStr            string `xorm:"varchar(64)"`                      // ["领取场景值str","show","input"]
	IsRestoreMemberCard int    `xorm:""`                                 // ["IsRestoreMemberCard","show","input"]
	IsRecommendByFriend int    `xorm:""`                                 // ["IsRecommendByFriend","show","input"]
	UnionId             string `xorm:"varchar(32)"`                      // ["UnionId","show","input"]

	//核销相关字段
	ConsumeData `xorm:"extends"`
}

type ConsumeData struct {
	//核销相关字段
	ConsumeSource string `xorm:""` // ["ConsumeSource","show","input"] 核销来源。支持开发者统计API核销（FROM_API）、公众平台核销（FROM_MP）、卡券商户助手核销（FROM_MOBILE_HELPER）（核销员微信号）
	LocationName  string `xorm:""` // ["LocationName","show","input"] 门店名称，当前卡券核销的门店名称（只有通过自助核销和买单核销时才会出现该字段）
	StaffOpenId   string `xorm:""` // ["StaffOpenId","show","input"] 核销该卡券核销员的openid（只有通过卡券商户助手核销时才会出现）
	VerifyCode    string `xorm:""` // ["VerifyCode","show","input"] 自助核销时，用户输入的验证码
	RemarkAmount  string `xorm:""` // ["RemarkAmount","show","input"]
}

func NewWxCardUserCardByGetCardEvent(weappid string, getCardEvt *wxcardlib.UserGetCardEvent) *WxCardUserCard {
	usercard := &WxCardUserCard{
		//userWho:userwho,
		WeappId: weappid,

		CardId:              getCardEvt.CardId,
		IsGiveByFriend:      getCardEvt.IsGiveByFriend,
		FriendUserName:      getCardEvt.FriendUserName,
		UserCardCode:        getCardEvt.UserCardCode,
		OldUserCardCode:     getCardEvt.OldUserCardCode,
		OuterId:             getCardEvt.OuterId,
		OuterStr:            getCardEvt.OuterStr,
		IsRestoreMemberCard: getCardEvt.IsRestoreMemberCard,
		IsRecommendByFriend: getCardEvt.IsGiveByFriend,
		UnionId:             getCardEvt.UnionId,
	}

	return usercard
}

func (t *WxCardUserCard) SetConsume(consumeEvt *wxcardlib.UserConsumeCardEvent) error {
	t.ConsumeData.ConsumeSource = consumeEvt.ConsumeSource
	t.ConsumeData.LocationName = consumeEvt.LocationName
	t.ConsumeData.StaffOpenId = consumeEvt.StaffOpenId
	t.ConsumeData.VerifyCode = consumeEvt.VerifyCode
	t.ConsumeData.RemarkAmount = consumeEvt.RemarkAmount
	t.Status = wxcardlib.UserCardStatusConsumed
	return nil
}
